package com.itrip.itripbiz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itrip.common.DtoUtil;
import com.itrip.common.Page;
import com.itrip.dto.Dto;
import com.itrip.entity.ItripComment;
import com.itrip.entity.ItripImage;
import com.itrip.service.ItripCommentService;
import com.itrip.service.ItripImageService;
import com.itrip.vo.ItripImageVO;
import com.itrip.vo.comment.ItripCountCommentVO;
import com.itrip.vo.comment.ItripListCommentVO;
import com.itrip.vo.comment.ItripScoreCommentVO;
import com.itrip.vo.comment.ItripSearchCommentVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/biz/api")
public class HotelCommentControler {

    @Resource
    private ItripCommentService itripCommentService;
    @Resource
    private ItripImageService itripImageService;

    //查询评论数量
    @GetMapping("/comment/getcount/{cityId}")
    public Dto<ItripComment> itripCommentDto(@PathVariable("cityId") Long id){
        ItripCountCommentVO itripCountCommentVO = itripCommentService.itripCountCommentVO(id);
        return DtoUtil.returnDataSuccess(itripCountCommentVO);
    }

    //查询酒店评分
    @GetMapping("/comment/gethotelscore/{cityId}")
    public Dto<ItripScoreCommentVO> itripCommentDtoScore(@PathVariable("cityId")  Long id){
        ItripScoreCommentVO ItripScoreCommentVO= itripCommentService.itripScoreCommentVo(id);
        return DtoUtil.returnDataSuccess(ItripScoreCommentVO);
    }
    //查询评论内容列表
    @PostMapping("/comment/getcommentlist")
    public Dto getCommentlist(@RequestBody ItripSearchCommentVO itripSearchCommentVO){

        List<ItripListCommentVO> itripCommentList = itripCommentService.getlistComment(itripSearchCommentVO);

        ItripCountCommentVO itripCountCommentVO = itripCommentService.itripCountCommentVO(itripSearchCommentVO.getHotelId());
        int pageCount = 0;
        if(itripSearchCommentVO.getIsHavingImg() ==-1 && itripSearchCommentVO.getIsOk() ==-1){
            pageCount = itripCountCommentVO.getAllcomment().intValue();
        }
        if(itripSearchCommentVO.getIsHavingImg() ==-1 && itripSearchCommentVO.getIsOk() == 1){
            pageCount = itripCountCommentVO.getIsok().intValue();
        }
        if(itripSearchCommentVO.getIsHavingImg()==-1 && itripSearchCommentVO.getIsOk() == 0){
            pageCount = itripCountCommentVO.getImprove().intValue();
        }
        if(itripSearchCommentVO.getIsHavingImg()== 1 && itripSearchCommentVO.getIsOk() == -1){
            pageCount = itripCountCommentVO.getHavingimg().intValue();
        }
        Page page = new Page(itripSearchCommentVO.getPageNo(),itripSearchCommentVO.getPageSize(),pageCount);

        page.setRows(itripCommentList);

        return DtoUtil.returnDataSuccess(page);
    }
    //查询评论附带的图片
    @GetMapping("/comment/getimg/{commentId}")
    public Dto getCommentimg(@PathVariable("commentId") Long id){
        ItripImageVO itripImageVO = new ItripImageVO();
        QueryWrapper qw = new QueryWrapper();
        qw.eq("type",2);
        qw.eq("targetId",id);
        qw.select("imgUrl","position");
        List<ItripImage> itripImage = itripImageService.list(qw);
        return DtoUtil.returnDataSuccess(itripImage);

    }
}
